package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.Elevator;
import com.example.entity.ElevatorFileInfo;
import com.example.entity.PointDefend;
import com.example.entity.PointFlow;
import com.example.mapp.ElevatorFileInfoMapp;
import com.example.mapp.ElevatorMapp;
import com.example.mapp.PointFlowMapp;
import com.example.service.ElevatorFile;
import com.example.util.ObjectBeanUtils;
import com.example.util.ResultUtil;
import com.example.util.SystemPointUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wang on 2018/3/31.
 */
@Service
public class ElevatorFileImpl implements ElevatorFile {

    private final static Logger logger = (Logger) LoggerFactory.getLogger(SysUserServiceImpl.class);


    @Autowired
    private ElevatorFileInfoMapp elevatorFileInfoMapp;

    @Autowired
    private PointFlowMapp pointFlowMapp;

    @Autowired
    private ElevatorMapp elevatorMapp;

    @Override
    public Map<String, Object> elevatorFileUpload(ElevatorFileInfo elevatorFileInfo) {

        try {
            ElevatorFileInfo eleNum = elevatorFileInfoMapp.queryByElevatorNum(elevatorFileInfo.getElevatorNum());
            if (null == eleNum) {
                elevatorFileInfoMapp.addElevatorFileInfo(elevatorFileInfo);
            } else {
                elevatorFileInfoMapp.update(elevatorFileInfo);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }finally {
            PointDefendInfoCache infoCache = new PointDefendInfoCache();
            PointDefend pointDefend = infoCache.getKeyByValue("elevator_technology_safe");
            if (pointDefend != null) {
                // 获取必填字段
                List<String> strings = Arrays.asList(pointDefend.getMustBean().split(","));
                double rate = ObjectBeanUtils.percentage(elevatorFileInfo, strings);
                if (rate != 0) {
                    // 单个电梯的完整积分
                    int round = (int) (rate * pointDefend.getMark());
                    // 通过设备Id获取信息
                    try {
                        Elevator elevator = elevatorMapp.queryByElevatorNum(elevatorFileInfo.getElevatorNum());

                        // 先将单个电梯的积分清空，然后再将新的积分更新
                        elevator.setTechniqueMark(round);
                        elevatorMapp.updateTechniqueMark(elevator);
                        // 计算单位下所有的电梯积分，得到单位总积分
                        Map sumTechniqueMark = elevatorMapp.getSumTechniqueMark(elevator.getUsedPartyId().toString());
                        int allElevator = Integer.parseInt(sumTechniqueMark.get("techniqueMark").toString());
                        // 查询角色信息得到单位总积分
                        Map<String, Object> maps = pointFlowMapp.findUserInfo(elevator.getUsedPartyId());
                        long mark = (long) maps.get("mark");

                        // 查询流水表里是否有记录了，如果有删除掉，再加
                        PointFlow pointFlow = new PointFlow();
                        pointFlow.setIdentification("elevator_technology_safe");
                        List<PointFlow> flowlist = pointFlowMapp.findOne(pointFlow);
                        if (flowlist != null && flowlist.size() > 0) {
                            round = (int) ((mark - flowlist.get(0).getMark()) + allElevator);
                        }else{
                            round = (int) (mark + allElevator);
                        }
                        // 更新单位总积分
                        String userId = (String) maps.get("userId");
                        int i1 = pointFlowMapp.updateMark(round, userId);
                        // 插入流水
                        String userName = (String) maps.get("userName");
                        String partyName = (String) maps.get("partyName");
                        String function = "完善电梯技术档案";
                        SystemPointUtil.insertFlow(pointFlowMapp, i1, pointDefend, allElevator, userName, partyName, function);


                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return ResultUtil.returnSuccess("上传文件成功",null);
    }

    @Override
    public Map<String, Object> elevatorFileQuery(String elevatorNum) {
        Map<String,Object> map = new HashMap<String,Object>();
        Object ret = null;
        try {
            ret = elevatorFileInfoMapp.queryByElevatorNum(elevatorNum);
            map.put("list",ret);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("查询成功",map);
    }
}
